package cn.gogoyc.product.service.impl;

import cn.gogoyc.org.basic.servie.IBaiduAiAuditService;
import cn.gogoyc.org.basic.servie.impl.BaseServiceImpl;
import cn.gogoyc.product.domain.Product;
import cn.gogoyc.product.domain.ProductDetail;
import cn.gogoyc.product.domain.ProductOnlineAuditLog;
import cn.gogoyc.product.mapper.ProductDetailMapper;
import cn.gogoyc.product.mapper.ProductMapper;
import cn.gogoyc.product.mapper.ProductOnlineAuditLogMapper;
import cn.gogoyc.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * ProductServiceImpl业务实现类:
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Override
    public void onsale(List<Long> ids) {
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());
            if (boo1 && boo2) {
                productMapper.updateOnsaleState(id, new Date());
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(product.getId());
                log.setState(1);
                log.setNote(id + "审核成功!");
                productOnlineAuditLogMapper.save(log);
            } else {
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(product.getId());
                log.setState(0);
                log.setNote(id + "审核失败!");
                productOnlineAuditLogMapper.save(log);
            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        productMapper.updateOffState(ids, new Date());
    }

    @Override
    public void save(Product product) {
        super.save(product);
        ProductDetail detail = product.getProductDetail();
        if (detail != null) {
            detail.setProductId(product.getId());
            productDetailMapper.save(detail);
        }
    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail detail = product.getProductDetail();
        detail.setProductId(product.getId());
        if (detail != null) {
            productDetailMapper.update(detail);
        }
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
        productDetailMapper.delProductDetailByProductId(id);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        ids.forEach(aLong -> remove(aLong));
    }
}
